<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta charset="UTF-8" name="viewport" content="initial-scale=1.0, user-scalable=no">
<title>后台管理</title>
<link rel="shortcut icon" href="static/image/logo01.ico"/>
<link href="static/easyui/themes/default/easyui.css" rel="stylesheet" />
<link href="static/easyui/themes/icon.css" rel="stylesheet" />
<script type="text/javascript" src="static/easyui/jquery.min.js"></script>
<script type="text/javascript" src="static/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="static/easyui-lang-zh_CN.js"></script>
</head>
<body>
<%
	String userId = request.getParameter("id");
%>
		<table>
			<tr>
				<td>用户名：</td>
				<td><input class="easyui-textbox" type="text" id="username" name="username" data-options="editable:false,readonly:true"></input></td>
			</tr>
			<tr>
				<td>年龄：</td>
				<td><input class="easyui-numberbox" type="text" id="age" data-options="min:0,max:100" name="age"></input></td>
			</tr>
			<tr>
				<td>手机号：</td>
				<td><input class="easyui-textbox" type="text" id="phone" name="phone"></input></td>
			</tr>
			<tr>
				<td>住址：</td>
				<td><input class="easyui-textbox" type="text" id="address" name="address"></input></td>
			</tr>
			<tr>
				<td>性别：</td>
				<td>
					<input type="radio" name="gender" checked value="男" />男
              		<input type="radio" name="gender" value="女" />女
				</td>
			</tr>
			<tr>
				<td>邮箱：</td>
				<td><input class="easyui-textbox" type="text" id="email" name="email"></input></td>
			</tr>
			<tr>
				<td>
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="submitForm()">提交</a>
				</td>
				<td>
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-reload'" onclick="clearForm()">重置</a>
				</td>
			</tr>
		</table>
		
	<script type="text/javascript">
		var phoneReg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
		var emailReg = /^[A-Za-zd0-9]+([-_.][A-Za-zd]+)*@([A-Za-zd]+[-.])+[A-Za-zd]{2,5}$/;
		$(function() {
			// 获取用户信息填充文本框
			$.ajax({
				url: "user/get",
				type: "get",
				data: {id: <%=userId%>},
				success: function(response) {
					console.log(response);
					if(response.code == 0) {
						$("#username").textbox('setValue', response.data.username);
						$("#age").numberbox('setValue', response.data.age);
						$("#phone").textbox('setValue', response.data.phone);
						$("#address").textbox('setValue', response.data.address);
						if (response.data.gender == '男') {
							$("input[name='gender'][value='男']").attr('checked','true');
						} else if (response.data.gender == '女') {
							$("input[name='gender'][value='女']").attr('checked','true');
						}
						$("#email").textbox('setValue', response.data.email);
					}
				}
			})
		})
		
		// 提交修改
		function submitForm() {
			var username = $("#username").val();
			var age = $("#age").numberbox('getValue');
			var phone = $("#phone").val();
			var address = $("#address").val();
			var gender = $("input[name='gender']:checked").val();
			var email = $("#email").val();
			
			
			if(!phoneReg.test(phone)) {
				$.messager.alert('提示', '请输入正确的手机号', 'error');
				return;
			}
			if(!emailReg.test(email)) {
				$.messager.alert('提示', '请输入正确的邮箱', 'error');
				return;
			}
			
			$.ajax({
				type : "post",
				url : "user/update",
				data : {
					id: "<%=userId%>",
					username : username,
					age: age,
					phone: phone,
					address: address,
					gender: gender,
					email: email
				},
				success : function(response) {
					if (response.code == 0) {
						$.messager.alert('提示信息',response.message,"info", function() {							
							parent.$("#topWindow").window("close");
						});
						
					} else {
						$.messager.alert('提示信息', response.message, "error");
					}
				}
			});

		}
		
		function clearForm() {
			$("#age").numberbox('reset');
			$("#phone").textbox('reset');
			$("#address").textbox('reset');
			$("#email").textbox('reset');
		}
	</script>
</body>
</html>